package com.mygame.client.handler;


import com.mygame.client.service.GameClientConfig;
import com.mygame.client.service.handler.GameClientChannelContext;
import com.mygame.client.service.handler.HeartbeatHandler;
import com.mygame.client.service.handler.codec.TcpExternalCodec;
import com.mygame.client.service.logichandler.SystemMessageHandler;
import com.mygame.common.component.BaseRoute;
import com.mygame.common.component.RequestPacket;
import com.mygame.common.component.annotation.Handler;
import com.mygame.common.component.annotation.Route;
import com.mygame.common.msg.CommandEnum;
import com.mygame.common.msg.ServiceEnum;
import com.mygame.common.utils.GameBase64Utils;
import com.mygame.common.utils.GameTimeUtil;
import com.mygame.common.utils.RSAUtils;
import com.mygame.common.protobuf.client.ConfirmRsp;
import com.mygame.common.protobuf.gameMsg.GetPlayerByIdResponse;
import com.mygame.common.protobuf.gameMsg.HeartbeatResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;

@Component
@Route(serviceType = ServiceEnum.CLIENT)
public class CommonHandler implements BaseRoute<RequestPacket> {

    @Autowired
    private GameClientConfig gameClientConfig;
    private static Logger logger = LoggerFactory.getLogger(SystemMessageHandler.class);

    @Override
    public void onReceive(RequestPacket packet) {
        System.out.println("CommonHandler");
    }

//    @GameMessageMetadata(messageId = 202, messageType = MessageType.REQUEST, serviceId = 101)

    //GATE_SERVER.CommonHandler.game.test
//    @Handler(cmd = CommandEnum.S2C_CON_FIRM)
//    public void S2C_CON_FIRM(GetPlayerByIdMsgRequest request,GameClientChannelContext ctx) {
//        logger.info("接收到客户端进入游戏请求：{}", request.getPlayerId());
//        EnterGameMsgResponse response = new EnterGameMsgResponse();
//        response.getBodyObj().setNickname("叶孤城");
//        response.getBodyObj().setPlayerId(1);
//        ctx.sendMessage(response);
//    }




    @Handler(cmd = CommandEnum.S2C_CON_FIRM)
    public void confirmResponse(ConfirmRsp response, GameClientChannelContext ctx) {
        String encryptAesKey = response.getSecretKey();
        byte[] content = Base64Utils.decodeFromString(encryptAesKey);
        try {
            byte[] privateKey = GameBase64Utils.decodeFromString(gameClientConfig.getRsaPrivateKey());
            byte[] valueBytes = RSAUtils.decryptByPrivateKey(content, privateKey);
            String value = new String(valueBytes);// 得到明文的aes加密密钥

            TcpExternalCodec tcpExternalCodec = ctx.getChannel().pipeline().get(TcpExternalCodec.class);
            tcpExternalCodec.setAesSecret(value);

            HeartbeatHandler heartbeatHandler = (HeartbeatHandler) ctx.getChannel().pipeline().get("HeartbeatHandler");
            heartbeatHandler.setConfirmSuccess(true);
            logger.debug("连接认证成功,channelId:{}",ctx.getChannel().id().asShortText());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Handler(cmd = CommandEnum.S2C_HEART_BEAT)
    public void S2C_HEART_BEAT(HeartbeatResponse response, GameClientChannelContext ctx) {
        logger.debug("服务器心跳返回，当前服务器时间：{}", GameTimeUtil.getStringDate(response.getServerTime()));
    }

    @Handler(cmd = CommandEnum.S2C_GET_PLAYER_BY_ID)
    public void S2C_GET_PLAYER_BY_ID(GetPlayerByIdResponse response, GameClientChannelContext ctx) {
        logger.debug("收到服务器返回消息，S2C_GET_PLAYER_BY_ID, playerId: {}", response.getPlayerId());
    }
}